package com.itboye.qihang;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {

    Button btn;
    Timer timer;
    int times=5;
//    Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//
//            }
//        }
//    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        btn= (Button) findViewById(R.id.btn);
        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                times--;
                if (times>0){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn.setText(times+"秒");
                        }
                    });

                }else {
                    timer.cancel();
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));
                    SplashActivity.this.finish();
                }
            }
        },0,1000);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                SplashActivity.this.finish();

            }
        });
    }


    @Override
    public void finish() {
        super.finish();
        timer=null;
    }
}
